package com.taxshare.seller.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.taxshare.mybatis.pager.PageHelp;
import com.taxshare.mybatis.pager.PageRequest;
import com.taxshare.response.beans.Response;
import com.taxshare.seller.entity.GoldenTaxPlateInfoEntity;
import com.taxshare.seller.mapper.GoldenTaxPlateMapper;
import com.taxshare.seller.util.InvoiceHelper;
import com.taxshare.seller.util.TransformationUtil;
import com.taxshare.seller.view.dto.GoldenTaxPlateDTO;
import com.taxshare.seller.view.vo.GoldenTaxPlateVO;
import com.taxshare.tools.util.DateUtil;
import com.taxshare.tools.util.UUIDUitl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: GoldenTaxPlateService
 * @Description:
 * @author: hjh
 * @date: 2021/5/31 16:09
 */
@Slf4j
@Service
@Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.READ_COMMITTED)
public class GoldenTaxPlateService extends ServiceImpl<GoldenTaxPlateMapper, GoldenTaxPlateInfoEntity> {

    @Autowired
    CommService commService;

    /**
     * 同步抄报信息
     *
     * @param map
     */
    public void syncGoldenTaxPlate(Map<String, Object> map) {
        HashMap map1 = (HashMap) map.get("COMMON_JSPXXCXS");
        HashMap<String, String> map2 = (HashMap) map1.get("COMMON_JSPXXCX");
        //处理发票类型，单张开票限额，离线剩余金额
        List<String> fpzldm = splitUtil(map2.get("FPZL_DM"), ",");
        List<HashMap<String, String>> dzkpxe = splitUtil(map2.get("DZKPXE"), ";");
        List<HashMap<String, String>> lxsyje = splitUtil(map2.get("LXSYJE"), ";");
        //根据返回的发票类型数向数据库同步信息
        for (int i = 0; i < fpzldm.size(); i++) {
            HashMap<String, String> dzkpxemap = dzkpxe.get(i);
            HashMap<String, String> lxsyjemap = lxsyje.get(i);
            GoldenTaxPlateInfoEntity goldenTaxPlateInfoEntity = new GoldenTaxPlateInfoEntity();
            goldenTaxPlateInfoEntity.setSellerName(map2.get("NSRMC"));
            goldenTaxPlateInfoEntity.setSellerTaxNo(map2.get("NSRSBH"));
            goldenTaxPlateInfoEntity.setFjh(map2.get("FJH"));
            goldenTaxPlateInfoEntity.setMachineNo(map2.get("JQBH"));
            goldenTaxPlateInfoEntity.setInvoiceTypeNo(TransformationUtil.transSstInvoiceType(fpzldm.get(i)));
            goldenTaxPlateInfoEntity.setStartTime(DateUtil.getFormatDate(map2.get("QYSJ")));
            goldenTaxPlateInfoEntity.setGoldenTaxPlateStatus(map2.get("JSPZT"));
            goldenTaxPlateInfoEntity.setLockDate(DateUtil.getFormatDate(map2.get("SSRQ")));
            goldenTaxPlateInfoEntity.setLastTaxDate(DateUtil.getFormatDate(map2.get("SCBSRQ")));
            goldenTaxPlateInfoEntity.setCopyTaxDate(DateUtil.getFormatDate(map2.get("CSQSRQ")));
            goldenTaxPlateInfoEntity.setIsCopyDate(map2.get("SFDBSQ"));
            goldenTaxPlateInfoEntity.setIsLockDate(map2.get("SFDSSQ"));
            goldenTaxPlateInfoEntity.setSingleQuota(dzkpxemap.get(fpzldm.get(i)));
            goldenTaxPlateInfoEntity.setOfflineTime(map2.get("LXSX"));
            goldenTaxPlateInfoEntity.setOfflineMoney(lxsyjemap.get(fpzldm.get(i)));
            goldenTaxPlateInfoEntity.setGoldenTaxPlateClock(DateUtil.getFormatDate(map2.get("JSPSZ")));
            goldenTaxPlateInfoEntity.setTaxReturnInfo(map2.get("BSZL"));
            QueryWrapper<GoldenTaxPlateInfoEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("seller_tax_no", map2.get("NSRSBH"))
                    .eq("invoice_type_no", TransformationUtil.transSstInvoiceType(fpzldm.get(i)));
            GoldenTaxPlateInfoEntity entity = baseMapper.selectOne(queryWrapper);
            if (entity == null) {
                goldenTaxPlateInfoEntity.setUuid(UUIDUitl.UUID32(false));
                this.save(goldenTaxPlateInfoEntity);
            } else {
                this.update(goldenTaxPlateInfoEntity, queryWrapper);
            }
        }
    }

    /**
     * 根据字符标识切割返回集合
     *
     * @param str
     * @param mark
     * @return
     */
    private <T> List splitUtil(String str, String mark) {
        String[] split = str.split(mark);
        List<String> list = Arrays.asList(split);
        //如果mark标识为";",则进行map处理
        if (";".equals(mark)) {
            List<HashMap<String, String>> collect = list.stream().map(item -> {
                String[] split1 = item.split(":");
                HashMap<String, String> map = new HashMap<>(8);
                map.put(split1[0], split1[1]);
                return map;
            }).collect(Collectors.toList());
            return collect;
        }
        return list;
    }

    /**
     * 抄报信息查询
     *
     * @param query
     * @return
     */
    public Response queryGoldenTaxPlate(PageRequest<GoldenTaxPlateDTO> query) {
        GoldenTaxPlateDTO info = query.getInfo();
        QueryWrapper<GoldenTaxPlateInfoEntity> queryWrapper = new QueryWrapper<>();
        if (!StrUtil.hasBlank(info.getSellerTaxNo())) {
            queryWrapper.eq("seller_tax_no", info.getSellerTaxNo());
        }else {
            List<String> taxNos = commService.getTaxNumbers();
            queryWrapper.in("seller_tax_no", taxNos);
        }
        queryWrapper.orderByDesc("create_time");
        IPage<GoldenTaxPlateVO> result = PageHelp.siamplePage(this, queryWrapper, query.getPage(), GoldenTaxPlateVO.class);
        result.getRecords().stream().forEach(
                item -> item.setInvoiceTypeNo(InvoiceHelper.ptransFplxdm(item.getInvoiceTypeNo()))
        );
        return Response.success(result);
    }
}


